package org.lzy.shop.order

import android.content.ClipData
import android.content.ClipboardManager
import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager

import org.lzy.shop.Constant
import org.lzy.shop.R
import org.lzy.shop.adapter.LogisticsListAdapter
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityLogisticsListBinding
import org.lzy.shop.util.MyUtil
import org.lzy.shop.util.ToastUtil
import org.lzy.shop.util.setUrlRound
import org.lzy.shop.viewmodel.OrderModel

/**
 * 查看物流信息
 */
class LogisticsListActivity : BaseMvvmActivity<ActivityLogisticsListBinding, OrderModel>({ ActivityLogisticsListBinding.inflate(it) })  {
    private  val logisticsListAdapter: LogisticsListAdapter by lazy { LogisticsListAdapter() }
    private var orderID = 0

    override fun initView() {
        setTitleText(getResString(R.string.logistics_info))
        if (intent.extras != null) {
            val bundle = intent.extras
            orderID = bundle!!.getInt(Constant.ORDERID)
        }
        binding.recyclerview.apply {
            layoutManager =LinearLayoutManager(this@LogisticsListActivity, LinearLayoutManager.VERTICAL, false)
            adapter = logisticsListAdapter
        }
    }
    
    override fun requestData() {
        mViewModel.getDefaultAddress()
        mViewModel.getLogistics(orderID)
    }

    override fun initData() {
        mViewModel.getDefaultAddData.observeWithLifecycle(this){addressResponse ->
            addressResponse?.let {
                val addressShow= StringBuilder()
                addressShow.append(addressResponse.province).append(addressResponse.city).append(addressResponse.district)
                binding.showAddressTxt.text = addressShow.toString()
                binding.showSpecaddressTxt.text=addressResponse.address
                binding.showContract.text=addressResponse.phone
            }
        }
        //物流列表
        mViewModel.logisticsrListData.observeWithLifecycle(this){logisticsResponse ->
            logisticsResponse?.let {
                binding.expressId.text=logisticsResponse.number
                binding.expressCompany.text=logisticsResponse.expName
                binding.courier.text=logisticsResponse.courier
                binding.expressTel.text=logisticsResponse.courierPhone
                binding.expressTime.text=logisticsResponse.takeTime
                setUrlRound(logisticsResponse.logo,20f,binding.expressImg)
                //物流轨迹
                logisticsResponse.list?.let {
                    if (logisticsResponse.list.isNotEmpty()) {
                        logisticsListAdapter.submitList(logisticsResponse.list)
                    }else{
                        showListEmpty()
                    }
                }
            }
        }
        
    }
    
    override fun allClick() {
        //打物流联系电话
        binding.expressTelCopy.setOnClickListener {
            if (!TextUtils.isEmpty( binding.expressTelCopy.text.toString())) {
                MyUtil.cellPhone(this@LogisticsListActivity, binding.expressTelCopy.text.toString())
            }
        }
        //物流单号复制
        binding.expressIdCopy.setOnClickListener {
            val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
            val clipData = ClipData.newPlainText(null, binding.expressIdCopy.text.toString())
            // 把数据集设置（复制）到剪贴板
            clipboard.setPrimaryClip(clipData)
            showToast(getResString(R.string.copy_success))
        }
    }


    fun showListEmpty() {
        logisticsListAdapter.submitList(null)
        logisticsListAdapter.isStateViewEnable = true
        logisticsListAdapter.stateView = showEmptyView()
    }


}